package com.shanghaionstar.home.presenter;

import android.text.TextUtils;
import android.util.Log;

import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.publicService.response.H5DispatcherResEntity;
import com.onstar.cn.mag.publicService.response.PreferentialResEntity;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.webview.handler.CheYinInsuranceHandler;
import com.shanghaionstar.activity.webview.handler.CommonWebViewActivity;
import com.shanghaionstar.activity.webview.handler.IWebType;
import com.shanghaionstar.activity.webview.handler.KuaiDaDiWebHandler;
import com.shanghaionstar.activity.webview.handler.WebDataEntity;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.home.iview.IPreferentialView;
import com.shanghaionstar.home.service.PreferentialService;
import com.shanghaionstar.utils.BannerUtil;
import com.shanghaionstar.utils.BusinessUtil;

import java.util.List;
import java.util.Map;

import rx.functions.Action1;

/**
 * Created by 7828wc on 16/6/29.
 */
public class PreferentialPresenter extends BasePresenter<Map<String, List<PreferentialResEntity>>> {
    private PreferentialService service;
    private IPreferentialView view;

    public PreferentialPresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<Map<String, List<PreferentialResEntity>>> view) {
        super.setView(view);
        service = new PreferentialService(view.onActivity());
        this.view = (IPreferentialView) view;
        setServiceProvider(service);
    }

    public void getMyDiscount(final String contentType) {
        BannerUtil bannerUtil = new BannerUtil(view.onActivity());
        view.setLoading(true);
        subscription = bannerUtil.commandData(contentType).subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread()).subscribe(new Action1<Map<String, List<PreferentialResEntity>>>() {
                    @Override
                    public void call(Map<String, List<PreferentialResEntity>> myRespones) {
                        view.setLoading(false);
                        if (myRespones == null || myRespones.isEmpty()) {
                            view.onNoData(contentType, true);
                            return;
                        }
                        view.onReponseSuccess(myRespones);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //抛出Error异常
                        view.setLoading(false);
                        view.onNoData(contentType, false);
                    }
                });
    }

    public void getH5(final PreferentialResEntity entity) {
        if (entity == null)
            return;
//        if (entity.getCategory().equalsIgnoreCase(CategoryType.CAR_MANAGER.value()) || (entity.getHttpMethod() != null && entity.getHttpMethod().equalsIgnoreCase("POST"))) {
//            launchCheYin(entity);
//            return;
//        }
        view.setLoading(true);
        BannerUtil bannerUtil = new BannerUtil(view.onActivity());
        subscription = bannerUtil.h5Dispatcher(entity).subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread()).subscribe(new Action1<H5DispatcherResEntity>() {
                    @Override
                    public void call(H5DispatcherResEntity mResult) {
                        String url = mResult.getUrl();
                        String body = mResult.getJsonStr();
                        view.setLoading(false);
                        if (entity.getHttpMethod()!=null && entity.getHttpMethod().equalsIgnoreCase("POST")) {
                            if (!TextUtils.isEmpty(url) && !TextUtils.isEmpty(body))
                                launchCheYin(entity);//prebook.
                        } else {
                            String title = entity.getTitle();
                            launchWeb(url, title);
                        }

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //抛出Error异常
                        view.setLoading(false);
                    }
                });
    }

    private void launchCheYin(PreferentialResEntity entity) {
//        Intent intent2 = new Intent(view.onActivity(), com.shanghaionstar.activity.webview.handler.CommonWebViewActivity.class);
//        Bundle bundle = new Bundle();
        WebDataEntity mWebData = new WebDataEntity();
        mWebData.what = IWebType.TYPE_CHE_YIN_INSURANCE;
        mWebData.setTopTitle(String.valueOf(entity.getTitle()));
        mWebData.setClzName(CheYinInsuranceHandler.class.getName());
        mWebData.setCanGoBack(true);
        com.shanghaionstar.activity.webview.handler.CommonWebViewActivity.launchCommonWeb(view.onActivity(), null, mWebData, entity);
//        bundle.putSerializable(IWebType.KEY_SERIAL_DATA,entity);
//        bundle.putParcelable(IWebType.KEY_WEB_DATA,mWebData);
//        intent2.putExtras(bundle);
////        intent2.putExtra(OnstarWebViewActivity.WEBVIEWS_PROXY_NAME, CheYinInsuranceWebViewProxy.class);
////        intent2.putExtra(CheYinInsuranceWebViewProxy.ENTITY, entity);
//        view.onActivity().startActivity(intent2);
    }

    public void launchWeb(String h5Url, String title) {
        if (TextUtils.isEmpty(h5Url)) {
            LogManager.getInstance().d("launchDetail...h5Url is blank");
            return;
        }
        WebDataEntity entity = new WebDataEntity();
        entity.setLinkUrl(h5Url);
        entity.setCanGoBack(true);
        entity.setTopTitle(title);
        if (h5Url.contains("kuaidadi")) {
            entity.setWhat(IWebType.TYPE_LOCATION);
            entity.setClzName(KuaiDaDiWebHandler.class.getName());
        } else
            entity.setWhat(IWebType.TYPE_NORMAL);
        if (view.onActivity().getString(R.string.tv_violation_title).equals(title))
            entity.setCanGoBack(true);
        CommonWebViewActivity.launchCommonWeb(view.onActivity(), entity);

//        Intent it = new Intent(view.onActivity(), CommonWebViewActivity.class);
//
//        it.putExtra("linkUrl", h5Url);
//        it.putExtra("requestLocation", h5Url.contains("kuaidadi"));
////        it.putExtra("topTitle", view.onActivity().getString(R.string.home_activity_preferential_detail_title));
//        view.onActivity().startActivity(it);
    }

    /**
     * 车音网调用
     *
     * @param
     */
//    public void launchCheYin(String url,String body) {
//        Intent intent2 = new Intent(view.onActivity(), OnstarWebViewActivity.class);
//        HashMap<String, String> webViewParam = new HashMap<>();
//        webViewParam.put(OnstarWebViewActivity.URL, url);
//        webViewParam.put(CheYinInsuranceWebViewProxy.PARAM_BODY, body);
//
//        intent2.putExtra(OnstarWebViewActivity.WEBVIEWS_PROXY_NAME, CheYinInsuranceWebViewProxy.class);
//        intent2.putExtra(OnstarWebViewActivity.WEBVIEWS_MAP,webViewParam);
//        view.onActivity().startActivity(intent2);
//    }
    public void launchDetail(PreferentialResEntity resp) {
        assert resp != null : Log.i("PreferentialPresenter", "resp is null");
        getH5(resp);
    }


    FunctionNameFindId[] promotionFindIds = {
            FunctionNameFindId.MyPrivilege_HotActivates1,
            FunctionNameFindId.MyPrivilege_HotActivates2,
            FunctionNameFindId.MyPrivilege_HotActivates3,
            FunctionNameFindId.MyPrivilege_HotActivates4,
            FunctionNameFindId.MyPrivilege_HotActivates5,
            FunctionNameFindId.MyPrivilege_HotActivates6,
            FunctionNameFindId.MyPrivilege_HotActivates7,
            FunctionNameFindId.MyPrivilege_HotActivates8,
            FunctionNameFindId.MyPrivilege_HotActivates9,
            FunctionNameFindId.MyPrivilege_HotActivates10,
    };

    public void onItemClick(PreferentialResEntity entity, int position) {
        if (position < promotionFindIds.length)
            BusinessUtil.sendReportByClickType(view.onActivity(), promotionFindIds[position].getStringValue());
        launchDetail(entity);
    }

    FunctionNameFindId[] commonFindIds = {FunctionNameFindId.MyPrivilege_hotclick1, FunctionNameFindId.MyPrivilege_hotclick2,
            FunctionNameFindId.MyPrivilege_hotclick3, FunctionNameFindId.MyPrivilege_hotclick4, FunctionNameFindId.MyPrivilege_hotclick5, FunctionNameFindId.MyPrivilege_hotclick6};

    public void onCommonItemClick(PreferentialResEntity entity, int position) {
        if (position < commonFindIds.length)
            BusinessUtil.sendReportByClickType(view.onActivity(), commonFindIds[position].getStringValue());
        launchDetail(entity);
    }

}
